package com.example.system.controller.exam;

import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.R;
import com.example.common.core.domain.TableDataInfo;
import com.example.common.core.enums.ResultCode;
import com.example.common.security.exception.ServiceException;
import com.example.system.domain.exam.dto.ExamAddDTO;
import com.example.system.domain.exam.dto.ExamEditDTO;
import com.example.system.domain.exam.dto.ExamQueryDTO;
import com.example.system.domain.exam.dto.ExamQuestionAddDTO;
import com.example.system.domain.exam.vo.ExamDetailVO;
import com.example.system.service.exam.IExamService;
import jakarta.servlet.ServletException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private IExamService examService;



    @GetMapping("/list")
    public TableDataInfo list(ExamQueryDTO examQueryDTO) {
        if (examQueryDTO.getPageNum() == null) {
            examQueryDTO.setPageNum(1);
        }
        if (examQueryDTO.getPageSize() == null) {
            examQueryDTO.setPageSize(10);
        }
        return getTableDataInfo(examService.list(examQueryDTO));
    }

    @PostMapping("/add")  //前端向后端传值用 于将 HTTP 请求中的 JSON、XML 等格式的数据反序列化为 Java 对象
    public R<String> add(@RequestBody ExamAddDTO examAddDTO){


        return R.ok(examService.add(examAddDTO));

    }

    @PostMapping("/question/add")
    public R<Void> questionAdd(@RequestBody ExamQuestionAddDTO examQuestionAddDTO){
        return toR(examService.questionAdd(examQuestionAddDTO));

    }

    @GetMapping("/detail")
    public R<ExamDetailVO> detail(Long examId){
        return R.ok(examService.detail(examId));

    }

    @PutMapping("/edit")
    public R<Void> edit(@RequestBody ExamEditDTO examEditDTO){
        return toR(examService.edit(examEditDTO));

    }

    @DeleteMapping ("/question/delete")
    public R<Void> delete(Long examId, Long questionId){
        return toR(examService.questionDelete(examId,questionId));

    }

    @DeleteMapping("/delete")
    public R<Void> delete(Long examId){
        return toR(examService.delete(examId));
    }

    @PutMapping("/publish")
    public R<Void> publish(Long examId){
        return toR(examService.publish(examId));
    }

    @PutMapping("/cancelPublish")
    public R<Void> cancelPublish(Long examId){
        return toR(examService.cancelPublish(examId));
    }


}
